package com.gxa.mall.controller;

import com.gxa.mall.common.CommonResult;
import com.gxa.mall.model.Admin;
import com.gxa.mall.service.LoginService;
import com.gxa.mall.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.*;

/**
 * @author 杨帅
 */
@RestController
@RequestMapping(path = "/login")
public class LoginController {
    @Autowired
    LoginService loginService;
    CommonResult commonResult=null;

    /**\
     * 用户登录验证
     * @param admin
     * @return
     */
    @RequestMapping(path = "/login")
    public CommonResult login(Admin admin,HttpServletRequest request){
        try {
            Admin admin1=loginService.login(admin);
            if(admin1!=null){
                /*生成token*/
                String token=JWTUtil.token(admin1.getId(),admin1.getAccount());
                request.getSession().setAttribute("token",token);
                admin1.setToken(token);
                commonResult=new CommonResult<Admin>(200,"登陆成功",admin1);
            }else {
                commonResult=new CommonResult(201,"账号或密码错误",admin1);
            }

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"登录异常",null);
        }
        return commonResult;
    }

    /**
     * 安全退出
     * @param
     * @return
     */
    @RequestMapping(path = "/exit")
    public void loginOut(HttpSession session){
        session.removeAttribute("admin");
    }

    /**
     * ajax表单验证判断此账号是否被注册
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(path = "/tocheck")
   public CommonResult tocheck(  Admin admin, HttpServletRequest request, HttpServletResponse response) {
       CommonResult commonResult=null;
        admin.setAccount(request.getParameter("account"));
       try{
           Integer num = loginService.tocheck(admin);
           commonResult=new CommonResult(200,"此账户可用",num);
       }catch (Exception e){
           e.printStackTrace();
           commonResult=new CommonResult(200,"此账户可用",null);
       }
       return commonResult;
    }






    /**
     * 用户注册
     * @param admin
     * @return
     */
    @RequestMapping(path = "/register")
    public CommonResult register(Admin admin){
        try {
         loginService.register(admin);
        if (admin!=null){
          commonResult=  new CommonResult<Admin>(200,"注册成功,请你去登录",admin);
        }else {
        commonResult=    new CommonResult<Admin>(201,"请输入账户密码",null);
        }

        }catch (Exception e){
            e.printStackTrace();
          commonResult=  new CommonResult<Admin>(500,"程序错误",null);
        }
        return commonResult;
    }

    /**
     * 修改密码
     * @param admin
     * @return
     */
    @RequestMapping(path = "/updatepassword")
    public CommonResult updatepassword(Admin admin){
        CommonResult commonResult=null;
        try {
            loginService.updatepassword(admin);
            System.out.println(admin);
            commonResult=new CommonResult(200,"密码修改成功",null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"错误,请稍后再试",null);
        }
        return commonResult;
    }

    /**
     * 点击个人信息页面,自动查询个人信息
     * @param account
     * @return
     */
    @RequestMapping(path = "/toseekadmin")
    public CommonResult toseekadmin(String account){
        CommonResult commonResult=null;
        try {
         Admin admin=   loginService.toseekadmin(account);
            commonResult=new CommonResult(200,"个人信息查询成功",admin);
        }catch (Exception e){
            commonResult=new CommonResult(500,"个人信息查询失败",null);
        }
        return commonResult;
    }


    @RequestMapping(path = "/updataadinfo")
    public CommonResult updataadinfo(Admin admin){
        CommonResult commonResult=null;
        try {
               loginService.updataadinfo(admin);
            commonResult=new CommonResult(200,"个人信息提交成功",null);
        }catch (Exception e){
            commonResult=new CommonResult(500,"个人信息提交失败",null);
        }
        return commonResult;
    }

}
